package com.fhnew.app.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
 *加密,MD5.toMD5(加密);
 */

public final class Md5 {

	public static String doubleToMd5(String str){
		return toMd5(toMd5(str));
	}
	public static String toMd5(String str) {
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte[] by = md.digest();// 
			String ss = "";
			String s;
			for (int i = 0; i < by.length; i++) {
				s = Integer.toHexString(0xFF & by[i]);
				if (s.length() == 1) {
					ss = ss + "0" + s;
				} else {
					ss = ss + s;
				}
			}
			return ss;
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}


	public static String digest(String value) {
		StringBuilder sb = null;
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			byte[] result = digest.digest(value.getBytes());
			sb = new StringBuilder();
			for (byte b : result) {
				String hexString = Integer.toHexString(b & 0xFF);
				if (hexString.length() == 1) {
					sb.append("0" + hexString);// 0~F
				} else {
					sb.append(hexString);
				}
			}
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException(e);
		}
		return sb.toString();
	}
}
